package com.chinasofti.listener;

import com.chinasofti.dao.CounterDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class Visitcount implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        CounterDao dao = new CounterDao();
        int visitcount = dao.selectvisitcount();
        ServletContext ctxt = servletContextEvent.getServletContext();
        ctxt.setAttribute("visitcount",visitcount);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        ServletContext ctxt = servletContextEvent.getServletContext();
        Integer visitcount = (Integer) ctxt.getAttribute("visitcount");
        CounterDao dao = new CounterDao();
        dao.updatevisitcount(visitcount);
    }
}